package com.example.boos.ui.storeInfo;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.boos.HttpUtil.MyPost;
import com.example.boos.MainActivity;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.pojo.Boss;
import com.example.boos.pojo.Store;
import com.example.boos.service.RabbitService;
import com.example.boos.stores.stores_activity;
import com.example.boos.ui.home.HomeFragment;
import com.example.boos.updateStoreAddr.UpdateStoreAddr;
import com.example.boos.util.MyThreadPool;
import com.example.boos.util.ShowPopuWIndow;

import java.util.Map;

public class MyStoreInfoFragment extends Fragment {

    private View view;
    private ImageView storeImage;
    private TextView showStoreName;
    private TextView showStoreInfo;
    private TextView updateStoreAddr;
    private TextView updateStoreName;
    private TextView updateStoreInfo;
    private TextView updateStoreType;
    private TextView selectStore;

    private String url = "updateStore";

    private PopupWindow popupWindow;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            Boolean ack = (Boolean)msg.obj;
            switch (msg.what){
                case 1:
                    if(ack){
                        Toast.makeText(MainActivity.getMainActivity(),"店铺名更新成功",Toast.LENGTH_SHORT).show();
                        // 店名修改完毕，显示新的店名
                        showStoreName.setText(BossInfo.currentStore.getStoreName());
                        popupWindow.dismiss();
                    }else{
                        Toast.makeText(MainActivity.getMainActivity(),"店铺名更新失败，请重试",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2:
                    // 更新店铺描述
                    if(ack){
                        Toast.makeText(MainActivity.getMainActivity(),"店铺描述更新成功",Toast.LENGTH_SHORT).show();
                        showStoreInfo.setText(BossInfo.currentStore.getStoreInfo());
                        popupWindow.dismiss();
                    }else{
                        Toast.makeText(MainActivity.getMainActivity(),"店铺描述更新失败，请重试",Toast.LENGTH_SHORT).show();
                    }
                    break;

            }
        }
    };



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mystore_info,container,false);
        initView(view);
        initData();
        initListener();
        return view;
    }

    private void initView(View view){
        storeImage = view.findViewById(R.id.update_storeImage);
        showStoreName = view.findViewById(R.id.update_showStoreName);
        showStoreInfo = view.findViewById(R.id.update_showStoreInfo);
        updateStoreAddr = view.findViewById(R.id.update_storeAddr);
        updateStoreName = view.findViewById(R.id.update_storeName);
        updateStoreInfo = view.findViewById(R.id.update_storeInfo);
        updateStoreType = view.findViewById(R.id.update_storeType);
        selectStore = view.findViewById(R.id.updaet_selectStore);
    }
    private void initData(){
        // 设置数据
        Glide.with(view).load(BossInfo.currentStore.getStoreImage()).into(storeImage);
        showStoreName.setText(BossInfo.currentStore.getStoreName());
        showStoreInfo.setText(BossInfo.currentStore.getStoreInfo());
    }
    private void initListener(){
        // 设置监听器
        // 选择店铺
        selectStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Intent intent1 = new Intent(MainActivity.getMainActivity(), RabbitService.class);
                MainActivity.getMainActivity().stopService(intent1); // 关闭服务
                intent.setComponent(new ComponentName(MainActivity.getMainActivity(), stores_activity.class));
                MainActivity.getMainActivity().startActivity(intent);
                HomeFragment.view = null;
                MainActivity.getMainActivity().finish();
            }
        });

        // 店铺类型的修改就先放着吧
        updateStoreType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        // 修改店铺描述
        updateStoreInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> result = ShowPopuWIndow.showPop(R.layout.popue_update_store_info, MainActivity.getMainActivity());
                View view = (View) result.get("view");
                popupWindow = (PopupWindow) result.get("popWindow");
                ((TextView)view.findViewById(R.id.update_showStoreInfo)).setText(BossInfo.currentStore.getStoreInfo());
                EditText newStoreInfo = view.findViewById(R.id.update_newSToreInfo);
                // 设置监听器
                // 取消按钮
                (view.findViewById(R.id.update_cancle)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                // 提交按钮
                (view.findViewById(R.id.update_ack)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String info  = newStoreInfo.getText().toString();
                        info = info.trim();
                        if("".equals(info)){
                            Toast.makeText(MainActivity.getMainActivity(),"店铺描述不能为空！",Toast.LENGTH_SHORT).show();
                            return ;
                        }
                        Store store = new Store();
                        store.setId(BossInfo.currentStoreId);
                        store.setStoreInfo(info);
                        MyThreadPool.doJob(new Runnable() {
                            @Override
                            public void run() {
                                Boolean ack = (Boolean) MyPost.doPost(url,store);
                                Message msg = new Message();
                                if(ack){
                                    BossInfo.currentStore.setStoreInfo(store.getStoreInfo());
                                }
                                msg.what = 2;
                                msg.obj = ack;
                                handler.sendMessage(msg);
                            }
                        });
                    }
                });
            }
        });
        // 修改店铺图片
        storeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowPopuWIndow.showPopueWindow(MainActivity.getMainActivity(),3);
            }
        });
        // 修改地址
        updateStoreAddr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到 UpdateStoreAddr
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(MainActivity.getMainActivity(), UpdateStoreAddr.class));
                MainActivity.getMainActivity().startActivity(intent);
            }
        });
        // 修改店名
        updateStoreName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> result = ShowPopuWIndow.showPop(R.layout.popue_update_store_name, MainActivity.getMainActivity());
                View view = (View) result.get("view");
                popupWindow = (PopupWindow) result.get("popWindow");
                // 设置数据
                ((TextView)view.findViewById(R.id.update_oldStoreName)).setText(BossInfo.currentStore.getStoreName());
                (view.findViewById(R.id.update_cancle)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                ((Button)view.findViewById(R.id.update_ack)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String newStoreName = ((EditText)view.findViewById(R.id.update_newStoreName)).getText().toString();
                        if("".equals(newStoreName)){
                            Toast.makeText(MainActivity.getMainActivity(),"请输入新的店铺名称",Toast.LENGTH_SHORT).show();
                            return ;
                        }
                        // 修改店铺信息
                        Store store = new Store();
                        store.setId(BossInfo.currentStoreId);
                        store.setStoreName(newStoreName);
                        MyThreadPool.doJob(new Runnable() {
                            @Override
                            public void run() {
                                Boolean ack = (Boolean) MyPost.doPost(url,store);
                                Message msg = new Message();
                                msg.what = 1;
                                msg.obj = ack;
                                if(ack){
                                    // 更新成功
                                    BossInfo.currentStore.setStoreName(newStoreName);
                                }
                                handler.sendMessage(msg);
                            }
                        });

                    }
                });
            }
        });
    }
}
